package cn.tedu.demo.controller;

import cn.tedu.demo.pojo.dto.UserLoginDTO;
import cn.tedu.demo.pojo.dto.UserRegDto;
import cn.tedu.demo.pojo.entity.User;
import cn.tedu.demo.pojo.vo.UserInfoVO;
import cn.tedu.demo.pojo.vo.UserVO;
import cn.tedu.demo.service.IUserService;
import cn.tedu.demo.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RequestMapping("/user")
@Api("用户登录模块")
@RestController
@CrossOrigin(origins = "http://localhost:9010",allowCredentials = "true")
public class UserController {
    @Autowired
    IUserService service;

    @PostMapping("/reg")
    @ApiOperation("用户注册模块")
    @ApiOperationSupport(order = 110)
    public JsonResult<Void> reg(UserRegDto user, HttpServletRequest request, HttpServletResponse response){
        log.debug("进入控制层，开始处理【注册用户】的请求");
        service.addUser(user,request,response);
        return JsonResult.ok();
    }
    @PostMapping("/login")
    @ApiOperation("用户登录模块")
    @ApiOperationSupport(order = 310)
    public JsonResult<UserVO> login(UserLoginDTO userLoginDTO,HttpServletRequest request, HttpServletResponse response){
        log.debug("进入控制层，开始处理【用户登录】的请求");
        log.debug("参数：{}",userLoginDTO);
        UserVO user = service.getUser(userLoginDTO,request,response);
        log.debug("session中的user：{}",user);
        return JsonResult.ok(user);
    }
    @PostMapping("/logout")
    public JsonResult<Void> logout(HttpServletRequest request){
        log.debug("进入控制层，开始处理【退出登陆】的请求");
        service.logout(request);
        log.debug("已退出登录");
        return JsonResult.ok();
    }
    @GetMapping("/currentUser")
    public JsonResult<UserInfoVO> currentUser(HttpSession session){
        log.debug("进入控制层，开始处理【获取当前用户】的请求");
        UserInfoVO userInfoVO = service.currentUser(session);
        log.debug("参数：{}",userInfoVO);
        return JsonResult.ok(userInfoVO);
    }

}
